package com.topcheer.common.auth.runner;

import com.topcheer.common.auth.config.UserAuthConfig;
import com.topcheer.common.auth.feign.RbacService;
import com.topcheer.common.base.vo.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

/**
 * @author xutao
 * @Package com.topcheer.common.auth.runner
 * @Description: ${todo}
 * @date 2020/4/8 14:43
 */
@Configuration
@Slf4j
@EnableScheduling
public class AuthClientRunner implements CommandLineRunner {
    @Autowired
    private RbacService rbacService;

    @Autowired
    UserAuthConfig userAuthConfig;
    @Override
    public void run(String... args) throws Exception {
        log.info("初始化加载用户pubKey");
        try {
            refreshUserPubKey();
        }catch(Exception e){
            log.error("初始化加载用户pubKey失败,1分钟后自动重试!",e);
        }

    }
    @Scheduled(cron = "0 0/1 * * * ?")
    public void refreshUserPubKey(){
        BaseResponse<byte[]> resp = rbacService.getUserPubKey();
        if (resp.getCode() == HttpStatus.OK.value()) {
            this.userAuthConfig.setPubKeyByte(resp.getData());
        }
    }
}
